package com.mofang.powerdekorhelper.activity.news;

import android.app.Dialog;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.ArticleCommentAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.model.ArticleComment;
import com.mofang.powerdekorhelper.model.NewsInfoDetial;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.persenter.NewsInfoDetialPersenter;
import com.mofang.powerdekorhelper.utils.DialogUtils;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.NetUtiles;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.NewsInfoDetialView;
import com.mofang.powerdekorhelper.witget.FullyLinearLayoutManager;
import com.mofang.powerdekorhelper.witget.MyWebViewClient;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.io.File;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by luofei on 2017/7/5 0005.
 */


public class NewsInfoDetialActivity extends MvpActivity<NewsInfoDetialView, NewsInfoDetialPersenter> implements NewsInfoDetialView {

    @BindView(R.id.news_info_detail_title)
    TextView titleTv;

    @BindView(R.id.news_info_detail_author)
    TextView authorTv;

    @BindView(R.id.news_info_detail_titlebar)
    TitleBar mTitleBar;

   /* @BindView(R.id.news_info_detail_imv)
    ImageView imageView;*/

    @BindView(R.id.news_info_detail_time)
    TextView timeTv;

    @BindView(R.id.news_info_detail_webview)
    WebView mWebView;

    @BindView(R.id.article_ditail_comment_reclcle)
    RecyclerView commentRecycle;

    private String articleId;

    //private int factoryId;

    private int page = 1;

    private int pageSize = 5;

    private String qrImageUrl;

    private Dialog sharedDialog = null;

    private LoadPrograss loadPrograss;

    private ArticleCommentAdapter adapter;

    private File localQrcodeImage;

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_news_info_detial_layout);

        initTitleBarWithback(mTitleBar, R.string.aticle_detial_title);

        loadPrograss = new LoadPrograss(this);

        commentRecycle.setLayoutManager(new FullyLinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
    }

    @Override
    public void initData() {
        articleId = getIntent().getStringExtra("ArticleId");
        presenter.getNewsInfoDetial(articleId);
        //factoryId = SharePerforenceUtils.getInstance(this).getFactoryId();
    }

    @Override
    public void setNewsInfoDetial(NewsInfoDetial newsInfoDetial) {

        if (newsInfoDetial.getResult() != null) {

            titleTv.setText(newsInfoDetial.getResult().getTitle());
            authorTv.setText(newsInfoDetial.getResult().getAuthor());
            timeTv.setText(newsInfoDetial.getResult().getArticle_createTime1());

            // PicassoUtils.setImageUrl(this, newsInfoDetial.getResult().getImage(), imageView);

            String articleHtml = newsInfoDetial.getResult().getDetails();

            WebSettings webSettings = mWebView.getSettings();//获取webview设置属性
            webSettings.setJavaScriptEnabled(true);//支持js
            mWebView.setWebViewClient(new MyWebViewClient());
            mWebView.loadDataWithBaseURL(null, articleHtml, "text/html", "utf-8", null);

            presenter.getArticleComment(page, pageSize, articleId);
        }

        //hideProgress();
    }

    @Override
    public void setArticleComment(ArticleComment articleComment) {
        if (articleComment.getResult() != null) {

            if (articleComment.getResult().getData() != null && articleComment.getResult().getData().size() > 0) {

                adapter = new ArticleCommentAdapter(articleComment.getResult().getData(), this, R.layout.article_comment_list_item_layout);

                commentRecycle.setAdapter(adapter);
            }
        }

        //hideProgress();
    }

    @Override
    public void setQrCode(ResultMessage4 resultMessage) {

        if (resultMessage.getResult() != null && !resultMessage.getResult().equals("")) {
            qrImageUrl = resultMessage.getResult();

            if (sharedDialog == null) {

                sharedDialog = DialogUtils.MySharedDialog(this, new ShareDialogListener());
            }
            sharedDialog.show();
        }

        showProgress();
        new NetUtiles().bitmapCorvert(this, qrImageUrl, R.string.share_news_title, "", titleTv.getText().toString(), new NetUtiles.ICorvertImage() {
            @Override
            public void corvertSuccess(File file) {
                localQrcodeImage = file;
                hideProgress();
            }

            @Override
            public void covertFail() {

            }
        });

    }

    class ShareDialogListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {

                case R.id.share_moments_imv:

                   /* new ShareUtils(NewsInfoDetialActivity.this)
                            .shareImage(NewsInfoDetialActivity.this, SHARE_MEDIA.WEIXIN_CIRCLE, qrImageUrl);*/

                   /* new ShareUtils(NewsInfoDetialActivity.this)
                            .shareLocalImage(NewsInfoDetialActivity.this, SHARE_MEDIA.WEIXIN_CIRCLE, localQrcodeImage, titleTv.getText().toString());*/


                    break;

                case R.id.share_wechat_imv:

                    /*new ShareUtils(NewsInfoDetialActivity.this)
                            .shareImage(NewsInfoDetialActivity.this, SHARE_MEDIA.WEIXIN, qrImageUrl);*/

                   /* new ShareUtils(NewsInfoDetialActivity.this)
                            .shareLocalImage(NewsInfoDetialActivity.this, SHARE_MEDIA.WEIXIN, localQrcodeImage, titleTv.getText().toString());*/

                    break;

                case R.id.share_sina_imv:

                    /*new ShareUtils(NewsInfoDetialActivity.this)
                            .shareLocalImage(NewsInfoDetialActivity.this, SHARE_MEDIA.SINA, localQrcodeImage, titleTv.getText().toString());*/

                    break;

                case R.id.share_cancle_tv:
                    sharedDialog.dismiss();
                    break;
            }
            sharedDialog.dismiss();
        }
    }

    @OnClick({R.id.share_article_continer})
    void click(View v) {
        switch (v.getId()) {

            case R.id.share_article_continer:

                //presenter.getQrCode(id, factoryId, 2);
                break;
        }
    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
    }

    @Override
    public void toast(CharSequence s) {

        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {
        T.showShort(this, getResources().getString(id));
    }

    @Override
    public void showNullLayout() {
        loadPrograss.hide();
    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        loadPrograss.hide();
    }

    @Override
    public void hideErrorLayout() {
        loadPrograss.hide();
    }

    @Override
    public NewsInfoDetialPersenter initPresenter() {
        return new NewsInfoDetialPersenter();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mTitleBar.setDividerColor(getResources().getColor(R.color.grey_deep));
    }

    @Override
    public void onError(String errorInfo) {

    }
}
